/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.client.actions;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import listo.client.ContextManager;
import listo.client.Lang;
import listo.client.MainForm;
import listo.client.Preferences;
import listo.client.model.Context;
import listo.client.model.operations.Operation;
import listo.utils.FileUtils2;
import listo.utils.MiscUtils;
import listo.utils.logging.Log;

import javax.swing.*;
import java.awt.event.ActionEvent;

public class LoadAction extends BaseAction {

    private final Preferences preferences;
    private final String defaultDataResource;
    private final ContextManager contextManager;
    private final MainForm mainForm;

    @Inject
    protected LoadAction(Log log, Lang lang, Preferences preferences,
                         @Named("DEFAULT_DATA_RESOURCE")String defaultDataResource,
                         ContextManager contextManager, MainForm mainForm) {
        super(log, lang, null);
        this.preferences = preferences;
        this.defaultDataResource = defaultDataResource;
        this.contextManager = contextManager;
        this.mainForm = mainForm;
    }

    /**
     * Invoked when an action occurs.
     */
    public void perform(ActionEvent e) {
        final String dataFile = preferences.getDataFile();
        boolean loaded = contextManager.load(dataFile);
        if (!loaded) {
            String defaultFile = FileUtils2.readAllTextFromResource(defaultDataResource);
            FileUtils2.writeAllText(defaultFile, dataFile);
            if (!contextManager.load(dataFile)) {
                contextManager.clearAll();
            }

            JOptionPane.showMessageDialog(mainForm, MiscUtils.wordWrap(String.format(
                    "A new data file %s has been created and filled with some demo content.\n" +
                            "When you are done exploring lis.to for the first time use \"File/Clear All\" " +
                            "to begin managing your own tasks.\n\nHave fun!", dataFile), 80),
                    "Welcome to lis.to!", JOptionPane.INFORMATION_MESSAGE);
        }

        // check for default folder
        Context currentContext = contextManager.getCurrentContext();
        if (currentContext.getFolderByPathName(preferences.getDefaultFolder()) == null &&
                JOptionPane.showConfirmDialog(mainForm, String.format(
                        "The default folder for new tasks \"%s\" does not exist.\nWould you like to create it?",
                        preferences.getDefaultFolder()), "Create default folder?",
                        JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {

            contextManager.createFolderPath(preferences.getDefaultFolder(), "inbox");
        }

        // add a save handler that saves after every context change
        contextManager.addContextChangedListener(new ContextManager.ContextChangedListener() {
            // CAUTION: This method is being called from the context thread.
            public void currentContextChanged(Context context, Operation undoOp, Operation redoOp) {
                contextManager.save(dataFile);
            }
        });
    }
}